//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.sunmi.sbs.commons.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;



public class LoginUtilsHandler {
    public static String propertiesFileRoute;
    public static JSONObject envConfigs;
    public static JSONObject apiConfigData;
    public static String env;
    public static String currentPath;
    public static JSONArray envArray = JSONArray.parseArray("[\"dev\", \"test\", \"pre\", \"onl\"]");

    public static JSONObject readProperties(String filePath) {
        Properties properties = new Properties();
        JSONObject root = new JSONObject();

        try (FileInputStream fis = new FileInputStream(filePath)) {
            properties.load(fis);

            // 遍历所有属性，按层级构建JSONObject
            properties.forEach((key, value) -> {
                String[] keys = key.toString().split("\\."); //
                JSONObject current = root;

                for (int i = 0; i < keys.length; i++) {
                    String k = keys[i];
                    boolean isLast = i == keys.length - 1;

                    if (!current.containsKey(k)) {
                        current.put(k, isLast ? value.toString() : new JSONObject());
                    }

                    // 进入下一层级
                    if (!isLast) {
                        current = current.getJSONObject(k);
                    }
                }
            });

            return root;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public LoginUtilsHandler(String env, String currentPath) throws Exception {
        LoginUtilsHandler.propertiesFileRoute = "src/main/resources/loginUtilsConfigs.properties";
        LoginUtilsHandler.env = env;
        LoginUtilsHandler.currentPath = currentPath;

        String propertiesInfo2Str = "{\"desInfo\":{\"pre\":{\"Iv\":\"98765432\",\"Key\":\"jihexxkj\"},\"dev\":{\"Iv\":\"12345678\",\"Key\":\"wywmxxkj\"},\"test\":{\"Iv\":\"12345678\",\"Key\":\"wywmxxkj\"},\"onl\":{\"Iv\":\"98765432\",\"Key\":\"jihexxkj\"}},\"apiPathConfig\":{\"rocRoute\":{\"loginByLoginName\":\"\"}},\"jdbcInfo\":{\"sg\":{\"pre\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"},\"test\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"},\"onl\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"}},\"th\":{\"pre\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"},\"test\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"},\"onl\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"}},\"cn\":{\"pre\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"},\"test\":{\"password\":\"XeDnrgWcvWJ12CBVKtu2\",\"url\":\"jdbc:mysql://middleware.base.db.test.sunmi.com:3306/roc_test?useSSL=true\",\"username\":\"qa_roc\"},\"onl\":{\"password\":\"\",\"url\":\"\",\"username\":\"\"}}},\"hostInfo\":{\"pre\":{\"idCenter\":{\"host\":\"webapi.uat.sunmi.com\"},\"maxiot\":{\"host\":\"api.pre.maxiot.com\"},\"max\":{\"host\":\"api-cn1.pre.biot-apps.com\"}},\"test\":{\"idCenter\":{\"host\":\"webapi.test.sunmi.com\"},\"max\":{\"host\":\"api-cn1.test.biot-apps.com\"},\"maxiot\":{\"host\":\"api.test.maxiot.com\"}},\"dev\":{\"idCenter\":{\"host\":\"webapi.dev.sunmi.com\"},\"max\":{\"host\":\"api-cn1.dev.biot-apps.com\"},\"maxiot\":{\"host\":\"api.dev.maxiot.com\"}},\"onl\":{\"idCenter\":{\"host\":\"webapi.sunmi.com\"},\"max\":{\"host\":\"api-cn1.biot-apps.com\"},\"maxiot\":{\"host\":\"api.maxiot.com\"}}},\"getFieldAccessorySQL\":\"\",\"loginPath\":{\"exchangeUserToken\":\"/auth/exchangeUserToken\",\"getAccessToken\":\"/auth/token/getAccessToken\",\"loginByLoginName\":\"/account/login/loginByLoginName\",\"login\":\"/public/v1/idcenter/user/login\"}}";
//        JSONObject propertiesInfo = readProperties(propertiesFileRoute);
        JSONObject propertiesInfo = JSONObject.parseObject(propertiesInfo2Str);
        apiConfigData = propertiesInfo.getJSONObject("apiPathConfig");
        System.out.println("propertiesInfo : "+ propertiesInfo);
        envConfigs = propertiesInfo;
        if (!envArray.contains(env)) {
            throw new Exception("**** env is Error! ****");
        } else {
            System.out.println("当前环境 ： " + env);
            System.out.println("当前api配置 : " + apiConfigData.toString());
            System.out.println("当前config配置 : " + envConfigs.toString());
        }
    }

    public JSONObject getJDBCInfo(String region) throws Exception {
        try {
            JSONObject jdbcInfo = envConfigs.getJSONObject("jdbcInfo").getJSONObject(region).getJSONObject(env);
            return jdbcInfo;
        } catch (Exception var3) {
            throw new Exception("getJDBCInfo异常 : " + var3);
        }
    }

    public static JSONObject apiPathConfigs() {
        JSONObject apiPathConfigsInfo = new JSONObject();
        String rocPath2Str = "/123/123, /456/456";
        JSONArray rocPath = str2JSONArray(rocPath2Str);
        apiPathConfigsInfo.put("roc", rocPath);
        return apiPathConfigsInfo;
    }

    private static JSONArray str2JSONArray(String args) {
        JSONArray arrayList = new JSONArray();
        String[] parts = args.split(", ");
        String[] var3 = parts;
        int var4 = parts.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String part = var3[var5];
            String element = part.trim();
            arrayList.add(element);
        }

        return arrayList;
    }
}
